/**
 * 
 */
package com.wiquery.plugin.raty;

import org.apache.wicket.markup.html.IHeaderContributor;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.panel.Panel;
import org.odlabs.wiquery.core.IWiQueryPlugin;
import org.odlabs.wiquery.core.javascript.JsQuery;
import org.odlabs.wiquery.core.javascript.JsStatement;
import org.odlabs.wiquery.core.options.Options;

import com.wiquery.plugin.raty.img.Images;

/**
 * @author Ernesto
 *
 */
public class RatingPanel extends Panel implements IWiQueryPlugin, IHeaderContributor {

	private static final long serialVersionUID = 1L;

	private WebMarkupContainer rating;
	
	private Options options;
	
	private int value;
	
	
	/**
	 * @param id
	 */
	public RatingPanel(String id, int stars, int initialValue) {
		super(id);
		this.value = initialValue;
		options = new Options();		
		rating = new WebMarkupContainer("rating");
		rating.setOutputMarkupId(true);
		add(rating);
		String url = urlFor(Images.IMG, null).toString();
		url = url.substring(0, url.lastIndexOf('/'));
		options.putLiteral("path", url+"/");
		options.put("number",stars);
		options.put("start", initialValue);
	}

	
	@Override
	public void renderHead(IHeaderResponse response) {
		super.renderHead(response);
		response.renderJavaScriptReference(RatingJavascriptResourceReference.get());
	}
	
	public RatingPanel setReadOnly(boolean readOnly) {
		options.put("readOnly", readOnly);
		return this;
	}

	public JsStatement statement() {
		return new JsQuery(rating).$().chain("raty", options.getJavaScriptOptions());
	}

	public int getValue() {
		return value;
	}

	public void setValue(int value) {
		this.value = value;
	}
}
